package com.jiepos.mpos.inter.intopieces.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.jiepos.mpos.common.constant.ExceptionConstant;
import com.jiepos.mpos.core.exception.MposException;
import com.jiepos.mpos.core.service.BaseInterService;
import com.jiepos.mpos.inter.intopieces.dto.RequestDTO;
import com.jiepos.mpos.inter.intopieces.dto.ResponseDTO;
import com.jiepos.mpos.inter.intopieces.service.MchntIntoPieces;
import com.jiepos.mpos.inter.intopieces.utils.FinalDataUtils;

/**
 * @ClassName: IntoPiecesController
 * @Description: 对外通道商户注册、查询、修改结算卡入口
 * @author 孙永
 * @date 2018年3月6日 下午5:04:08
 */
@RequestMapping("/mchnt")
@Controller
public class IntoPiecesController{
	
	private static Logger logger = LoggerFactory.getLogger(IntoPiecesController.class);
	
	@Autowired
	private BaseInterService baseInterService;
	@Autowired
	private MchntIntoPieces mchntIntoPieces;

	@RequestMapping(value = "/{service}.do", method = RequestMethod.POST)
	@ResponseBody
	public ResponseDTO register(HttpServletRequest request, HttpServletResponse response, RequestDTO requestDto, MultipartFile file, @PathVariable("service") String service){
		ResponseDTO responseDto = new ResponseDTO();
		Map<String, String> reqBizContent = null, resBizContent = null;
		try{
			logger.info("请求{}，报文：{}", service, requestDto.toString());
			/*提取请求业务参数*/
			reqBizContent = baseInterService.getRequestBizContent(requestDto);
			logger.info("请求{}，业务参数：{}", service, reqBizContent);
			if(FinalDataUtils.REGISTER.equals(service)){ // 注册服务
				resBizContent = mchntIntoPieces.register(reqBizContent);
			}else if(FinalDataUtils.SEARCH.equals(service)){ // 查询服务
				resBizContent = mchntIntoPieces.search(reqBizContent);
			}else if(FinalDataUtils.DEBITCARD.equals(service)){ // 变更结算卡服务
				resBizContent = mchntIntoPieces.debitCard(reqBizContent);
			}else if(FinalDataUtils.UPLOAD.equals(service)){ // 上送图片服务
				resBizContent = mchntIntoPieces.upload(request, response, reqBizContent, file);
			}else{
				throw new MposException(ExceptionConstant.MER_REQ_SERVICE_ERROR);
			}
			logger.info("{}业务处理结果：{}", service, resBizContent);
			/*应答参数签名加密*/
			String respData = baseInterService.getResponseBizContent(reqBizContent, resBizContent);
			responseDto.setData(respData);
		}catch(Exception e){
			e.printStackTrace();
			responseDto.setRsp_code(ExceptionConstant.MER_ERROR.getCode());
			responseDto.setRsp_msg(ExceptionConstant.MER_ERROR.getMessage());
			if(e instanceof MposException){
				responseDto.setRsp_code(((MposException) e).getCode());
				responseDto.setRsp_msg(e.getMessage());
			}
		}finally{
			baseInterService.saveLog(requestDto, responseDto, reqBizContent, resBizContent, service);
		}
		logger.info("应答{}，报文：{}", service, responseDto.toString());
		return responseDto;
	}

}
